package Backup;

// <editor-fold defaultstate="collapsed" desc="import">
import org.apache.log4j.*;
import Backup.Library.*;
import Backup.Source.Configuration.*;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import static java.util.Arrays.asList;
// </editor-fold>

public class CommandLineOptions extends Options
{
   
    // <editor-fold defaultstate="collapsed" desc="private varibles">
    private boolean _Verbose;
    private Action _Action;
    private String _SourceDirectory;
    private String _DestinationDirectory;
    private String _FtpAddress;
    private String _FtpUserName;
    private String _FtpPassword;
    private String _Revision;
    private PropertiesProvider _ApplicationStrings = new PropertiesProvider("Strings_en_GB.properties");
    private static Logger _Logger = Logger.getLogger(CommandLineOptions.class);
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="properties">
    @Override
    public Action getAction()
    {
        return this._Action;
    }    
    
    @Override
    public boolean getVerbose()
    {
        return this._Verbose;
    }
    
    @Override
    public String getSourceDirectory()
    {
        return this._SourceDirectory;
    }
     
    @Override
    public String getDestinationDirectory()
    {
        return this._DestinationDirectory;
    }
      
    @Override
    public String getFtpAddress()
    {
        return this._FtpAddress;
    }
        
    @Override
    public String getFtpUserName()
    {
        return this._FtpUserName;
    }
       
    @Override
    public String getFtpPassword()
    {
        return this._FtpPassword;
    }
        
    @Override
    public String getRevision()
    {
        return this._Revision;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="constructs">
    public CommandLineOptions(String[] arguments)
    {
        
        BasicConfigurator.configure();
        
        this.Parse(arguments);
        
        this.Validate();  
        
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="public methods">
    public String GetUsage()
    {
        return "Dupa";
        //throw new NotImplementedException();
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="private methods">
    private boolean Validate()
    {
        if(this._DestinationDirectory.isEmpty())
        {
            _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptyDestination"));
            return false;
        }
        else
        {
            super.setDestinationDirectory(this._DestinationDirectory);
        }  
      
        if(this._Action == Action.Backup)
        {
            if(this._FtpAddress.isEmpty())
            {
                _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptyFtpAddress"));
                return false;
            }
            else
            {
                super.setFtpAddress(this._FtpAddress);
            }
            
            if(this._FtpUserName.isEmpty())
            {
                _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptyFtpUserName"));
                return false;
            }
            else
            {
                super.setFtpUserName(this._FtpUserName);
            }
            
            if(this._FtpPassword.isEmpty())
            {
                _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptyFtpPassword"));
                return false;
            }
            else
            {
                super.setFtpPassword(this._FtpPassword);
            }                       
           
        }
        else if(this._Action == Action.Reroll)
        {
            if(this._SourceDirectory.isEmpty())
            {
                _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptySource"));
                return false;
            }
            else
            {
                super.setSourceDirectory(this._SourceDirectory);
            }
            
            if(this._Revision.isEmpty())
            {
                _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationEmptyRevision"));
                return false;
            }
            else
            {
                super.setRevision(this._Revision);
            }            
        }
        else
        {
            _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationWrongAction"));
            return false;
        }
        
        return true;
    }
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="override methods">
    @Override
    public void Parse(String[] arguments)
    {
        OptionParser OptionsParserInstance = new OptionParser();
        
        // program options
        OptionSpec<String> ActionOptionSpec = OptionsParserInstance.acceptsAll(asList("a", "action")).withRequiredArg().ofType( String.class );
        OptionsParserInstance.acceptsAll(asList("v", "verbose"));
        
        // options for both "Backup" and "Reroll" actions
        OptionSpec<String> SourceDirectory = OptionsParserInstance.acceptsAll(asList("s", "source")).withRequiredArg().ofType( String.class );
        OptionSpec<String> DestinationDirectory = OptionsParserInstance.acceptsAll(asList("d", "destination")).withRequiredArg().ofType( String.class );
        
        // options for "Backup" action
        OptionSpec<String> FtpAddress = OptionsParserInstance.acceptsAll(asList("f", "ftp")).withRequiredArg().ofType( String.class );
        OptionSpec<String> FtpUserName = OptionsParserInstance.acceptsAll(asList("u", "user")).withRequiredArg().ofType( String.class );
        OptionSpec<String> FtpPassword = OptionsParserInstance.acceptsAll(asList("p", "password")).withRequiredArg().ofType( String.class );

        // option for "Reroll" action
        OptionSpec<String> Revision = OptionsParserInstance.acceptsAll(asList("r", "revision")).withRequiredArg().ofType( String.class );
                
        // Parse arguments to option set
        OptionSet OptionSet = OptionsParserInstance.parse(arguments); 
        
        
        if(!OptionSet.valueOf(ActionOptionSpec).isEmpty())
        {
            // First leter in Action string to upper for further compare with Action enumerator
            char[] TempCharArray = OptionSet.valueOf(ActionOptionSpec).toCharArray();        
            TempCharArray[0] = Character.toUpperCase(TempCharArray[0]);
            this._Action = Action.valueOf(new String(TempCharArray));
        }
        else
        {
            _Logger.error(_ApplicationStrings.getString("ErrorOptionsValidationActionEmpty"));
        }        
        
        this._SourceDirectory = OptionSet.valueOf(SourceDirectory);
        this._DestinationDirectory = OptionSet.valueOf(DestinationDirectory);
        this._FtpAddress = OptionSet.valueOf(FtpAddress);
        this._FtpUserName = OptionSet.valueOf(FtpUserName);
        this._FtpPassword = OptionSet.valueOf(FtpPassword);
        this._Revision = OptionSet.valueOf(Revision);
        
        super.setAction(this._Action);
        super.setSourceDirectory(this._SourceDirectory);
        super.setRevision(this._Revision);
    }
    // </editor-fold>
    
}